**************************************************************************************************************************************************************************
* This replication file runs the code which calculates counterfactual welfare effects of a price reduction and maps out profitability under alternative pricing regimes. *
**************************************************************************************************************************************************************************
* 1. Determine a grid of price combinations which will be evaluated.
* 2. Run the code which calculates the change in welfare due to a price reduction and stores the relevant information.
* 3. Load the outcomes of the counterfactuals and find the welfare-maximizing combinations of price reductions for real-estate and other transactions.
* 4. Run the code which maps out firm profitability under the welfare-maximizing price combinations.

*************************
* 0 Set path to files *
*************************
clear all
capture log close
set more off
set trace off 
mata: mata set matastrict off
cd $main_directory


**********************************************************************
* 1. Determine a grid of price combinations which will be evaluated. *
**********************************************************************

global grid_start_Q1=0 //lower bound of % price reductions we consider for real-estate transactions 
global grid_end_Q1=35 //upper bound of % price reductions we consider for real-estate transactions 
global grid_start_Q2=0 //lower bound of % price reductions we consider for other transactions 
global grid_end_Q2=35 //upper bound of % price reductions we consider for other transactions 

************************************************************************************************************************
* 2. Run the code which calculates the change in welfare due to a price reduction and stores the relevant information. *
************************************************************************************************************************
forvalues reduction_Q1=`=$grid_start_Q1'(1)`=$grid_end_Q1' {
forvalues reduction_Q2=`=$grid_start_Q2'(1)`=$grid_end_Q2' {
global reduction_level_Q1 `reduction_Q1' //This sets the % price reduction for real estate transactions.
global reduction_level_Q2 `reduction_Q2' //This sets the % price reduction for other transactions.
do 07_02_price_reduction_function.do
}
}

********************************************************************************************************************************************************
* 3. Load the outcomes of the counterfactuals and find the welfare-maximizing combinations of price reductions for real-estate and other transactions. *
********************************************************************************************************************************************************

mata: n_reduction_combinations=(`=$grid_end_Q1'-`=$grid_start_Q1'+1)*(`=$grid_end_Q2'-`=$grid_start_Q2'+1)
mata: reduction_outcomes=J(n_reduction_combinations,10,.)

mata: counter=0
forvalues reduction_Q1=`=$grid_start_Q1'(1)`=$grid_end_Q1' {
forvalues reduction_Q2=`=$grid_start_Q2'(1)`=$grid_end_Q2' {
global reduction_level_Q1 `reduction_Q1' //This sets the % price reduction for real estate transactions.
global reduction_level_Q2 `reduction_Q2' //This sets the % price reduction for other transactions.
mata: counter=counter+1
mata: mata matuse "Estimates\Price_reduction\results_`=$reduction_level_Q1'_`=$reduction_level_Q2'", replace
mata: reduction_outcomes[counter,.]=results_`=$reduction_level_Q1'_`=$reduction_level_Q2'
}
}
mata: st_matrix("reduction_outcomes", reduction_outcomes)

matrix colnames reduction_outcomes = reduction_level_Q1 reduction_level_Q2 change_Q1_perc change_Q2_perc change_CS_Q1 change_CS_Q2 change_producer_surplus change_welfare unprofitable_firms producer_surplus_currentL
clear
svmat reduction_outcomes, names(col)
*Table 10, row 1
preserve
drop if reduction_level_Q1!=reduction_level_Q2
su change_welfare
sca max_welfare=r(max)
list if change_welfare==max_welfare
restore
*Table 10, row 2
preserve
su change_welfare
sca max_welfare=r(max)
list if change_welfare==max_welfare
restore

*Table 10, row 3
drop if producer_surplus<100000
preserve
drop if reduction_level_Q1!=reduction_level_Q2
su change_welfare
sca max_welfare=r(max)
list if change_welfare==max_welfare
restore

*Table 10, row 4
preserve
su change_welfare
sca max_welfare=r(max)
list if change_welfare==max_welfare
restore

*Table 10, row 5
drop if producer_surplus<200000
preserve
drop if reduction_level_Q1!=reduction_level_Q2
su change_welfare
sca max_welfare=r(max)
list if change_welfare==max_welfare
restore

*Table 10, row 6
preserve
su change_welfare
sca max_welfare=r(max)
list if change_welfare==max_welfare
restore

******************************************************************************************************
* 4. Run the code which maps out firm profitability under the welfare-maximizing price combinations. *
******************************************************************************************************

//Figure 3

//Current profitability
global reduction_level_Q1 0
global reduction_level_Q2 0
do 07_03_profitability_maps.do

//100 million guarantee
global reduction_level_Q1 27
global reduction_level_Q2 16
do 07_03_profitability_maps.do

//200 million guarantee
global reduction_level_Q1 21
global reduction_level_Q2 8
do 07_03_profitability_maps.do

